package com.fcscs.cservice.client.main.reservation;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.reservation.MembershipModelDate;
import com.fcscs.cservice.domain.model.ReservationView.Membership;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class MemberShipDialog extends BaseFormDialog<Membership> {
	private static MemberShipDialog INSTANCE;
	private MyFormPanel formPanel;
	private List<Membership> memList;
	private MySimpeGridPanel simGridPanel;
	private boolean isFirst;
	private String guestName;
	
	@Override
	protected FcsFormPanel<Membership> getFormPanel() {
		return formPanel;
	}
	
	public static MemberShipDialog getINSTANCE(String heading, List<Membership> memList, String guestName){
		if(null == INSTANCE){
			INSTANCE = new MemberShipDialog();
		}
		INSTANCE.memList = memList;
		INSTANCE.setHeading(heading);
		INSTANCE.guestName = guestName;
		return INSTANCE;
	}

	private MemberShipDialog(){
		isFirst = true;
		formPanel = new MyFormPanel(new FormSubmitListener<Membership>(){
			@Override
			public void formSubmitCallBack(FcsFormPanel<Membership> form,
					SubmitType submitType) {
			}
		});
		formPanel.setId("MemberShipDialog_formPanel");
		formPanel.setLabelWidth(120);
		add(formPanel);
		setSize(400, 300);
	}
	
	private class MySimpeGridPanel extends
	    SimpleGridPanel<MembershipModelDate>{
		public MySimpeGridPanel(String heading,
				ModelSelectListener<MembershipModelDate> modelSelectListener) {
			super(heading, null, (Object[]) null);

			setCollapsible(false);
			setSize(350, 160);
		}
		
		@Override
		protected ColumnModel createColumnConfig(Object... params) {
			List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			
			ColumnConfig columnConfig = CoreUtil.createNonSortableColumnConf(
				CServiceConstants.MEM_NO, CServiceConstants.CSERVICE_CONSTANTS.membershipNo(), 160);
			columns.add(columnConfig);

			columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.MEM_LV,
				CServiceConstants.CSERVICE_CONSTANTS.memLv(), 160));
			
			ColumnModel columnModel = new ColumnModel(columns);
			return columnModel;
		}
	}
	
	private class MyFormPanel extends FcsFormPanel<Membership>{
        private VerticalPanel vertPanel;
		
		private Label guestLabel;
		private AdapterField guestAdapterField;
		
		private FormButton okButton;	

		protected MyFormPanel(FormSubmitListener<Membership> l) {
			super(l);
			setHeaderVisible(false);
			setSize(380, 250);
			
			vertPanel = new VerticalPanel();
			vertPanel.setId("MemberShipDialog_vp_id");

			guestLabel = new Label();
			guestLabel.setId("MemberShipDialog_guestLabel_id");

			guestAdapterField = new AdapterField(guestLabel);
			guestAdapterField.setId("MemberShipDialog_guestAdapterField_id");
			guestAdapterField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
			add(guestAdapterField);
			
			simGridPanel = new MySimpeGridPanel(CServiceConstants.CSERVICE_CONSTANTS.memberInfo(),null);
			simGridPanel.setId("MemberShipDialog_simGridPanel_id");
			simGridPanel.setHeaderVisible(false);
			
			vertPanel.add(simGridPanel);
			add(vertPanel);
			
			okButton = new FormButton(CoreConstants.CORE_CONSTANTS.ok());
			okButton.setId("MemberShipDialog_okButton");
			okButton.setWidth(60);
			okButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "240px");
			okButton.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "10px");
			okButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					MemberShipDialog.this.hide();
				}
			});
			
			add(okButton);
		}

		@Override
		public void updateForm(Membership entity) {
			guestLabel.setText(guestName);
			for(Membership ms: memList){
				simGridPanel.addModel(new MembershipModelDate(ms));
			}
		}

		@Override
		public void populateEntity(Membership entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
	}

	@Override
	public void hide() {
		super.hide();
		if (!isFirst) {
			simGridPanel.removeAllModels();
		}
		isFirst = false;
	}
}
